package com.chezhujie.www.service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.chezhujie.www.core.page.Pagination;
import com.chezhujie.www.domain.Czjbusiness;
import com.chezhujie.www.domain.Trole;
import com.chezhujie.www.domain.Trolepermission;
import com.chezhujie.www.domain.Tsuser;
import com.chezhujie.www.persistence.TroleMapper;
import com.chezhujie.www.utils.UUIDUtils;



@Service("troleService")
public class TroleService {
	@Autowired
	private TroleMapper troleMapper;
	@Autowired
	private TrolepermissionService trolepermissionService;

	/**
	 * 查询某个角色
	 * @param param
	 * @return
	 */
	public Trole getTrole(Map<String,Object> param) {
		return troleMapper.getTrole(param);
	}
	
	/**
	 * 查询某个商家
	 * @param param
	 * @return
	 */
	public Czjbusiness getBussiness(Map<String,Object> param) {
		return troleMapper.getBussiness(param);
	}	
   
	/**
	 * 查询角色列表
	 * @return
	 */
	public Map<String,Object> getTroleList(Map<String,Object> param) {
		Map<String,Object> returnMap = new HashMap<String,Object>();
		returnMap.put("total", troleMapper.getTotalRows(param));
		returnMap.put("rows", troleMapper.getTroleList(param));
		return returnMap;
	}
   
	/**
	 * 查询角色列表
	 * @return
	 */
	public  Pagination getTroleList(Map<String,Object> param,int pageSize,Integer pageNo,String order,String sort) {
		Pagination p = new Pagination();
		p.setPageNo(pageNo);
		p.setPageSize(pageSize);
		p.setOrder(order);
		p.setSort(sort);
		param.putAll(p.toMap());
		int count=troleMapper.getTotalRows(param);
		List<Trole> list=	troleMapper.getTroleList(param);
	
		p.setList(list);
		p.setTotalCount(count);
		return p;
		
	}
      
	/**
	 * 新增角色
	 * @param trole
	 * @param param
	 */
	@Transactional
	public void insertTrole(Trole trole) {
		trole.setRoleid(UUIDUtils.getUUID());
		troleMapper.insertTrole(trole);
	}
   
	/**
	 * 编辑角色
	 * @param trole
	 * @param param
	 */
	@Transactional
	public void updateTrole(Trole trole) {
		troleMapper.updateTrole(trole);
	}
	
	/**
	 * 编辑角色
	 * @param trole
	 * @param param
	 */
	@Transactional
	public void updateBussiness(Czjbusiness czjbusiness) {
		troleMapper.updateBussiness(czjbusiness);
	}	
   
	/**
	 * 删除角色
	 * @param param
	 */    
	@Transactional
	public void deleteTrole(List<String> roleidList) {
		for(String roleid: roleidList){
	  		Map<String,Object> param = new HashMap<String,Object>();
	  		param.put("roleid", roleid);
	 	   troleMapper.deleteTrole(param);	  		
		}
	}

	public Trole get(String roleid) {
		// TODO Auto-generated method stub
		Map<String,Object> param = new HashMap<String,Object>();
  		param.put("roleid", roleid);
  		List<Trolepermission> trolepermissions=trolepermissionService.getList(param);
		Trole role = troleMapper.getTrole(param);
		if(role==null){
			return null;
		}
		Set<Trolepermission> set =new HashSet<Trolepermission>();
		if(trolepermissions!=null&&trolepermissions.size()!=0){
			set = new HashSet(trolepermissions);
		}
		
		role.setTrolepermissions(set);
		return role;
	}

	public List<Tsuser> getTroles() {
		return troleMapper.getTroles();
	}
	
	public List<Czjbusiness> getBussindessALL(Map<String,Object> param) {
		return troleMapper.getBussindessALL(param);
	}	


}